package middlewares

import (
	"github.com/gin-gonic/gin"
	"krm_backend/config"
	"krm_backend/utils/logs"
)

func JWTAuth(r *gin.Context) {
	// 除了login和logout其他接口都要验证token是否合法
	requestUrl := r.FullPath()
	logs.Debug(map[string]interface{}{"请求路径": requestUrl}, "")
	if requestUrl == "/api/auth/login" || requestUrl == "/api/auth/logout" {
		logs.Debug(map[string]interface{}{"请求路径": requestUrl}, "登录login和退出logout不需要验证token")
		r.Next()
		return
	}
	returnData := config.NewReturnData()
	// 验证token是否合法
	tokenString := r.Request.Header.Get("Authorization")
	if tokenString == "" {
		returnData.Status = 401
		returnData.Message = "请求未携带token，请登录后尝试"
		r.JSON(200, returnData)
		r.Abort()
		return
	}
}
